package com.lglin.common.utils.other;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.*;

public class CommonUtil {
    public static int[] splitData(String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String str = jsonObject.getString("id_arr");
        String newStr = str.substring(1, str.length() - 1);
        String[] strArray = newStr.split(",");
        return Arrays.stream(strArray).mapToInt(Integer::parseInt).toArray();
    }

    public static List<Integer> removeDuplicate(List<Integer> list) {
        List<Integer> result = new ArrayList<>(list.size());
        for (Integer str : list) {
            if (!result.contains(str)) {
                result.add(str);
            }
        }
        list.clear();
        list.addAll(result);
        return list;
    }

    public static String upload(MultipartFile uploadFile,String path){

        if(uploadFile.isEmpty()){
            //返回选择文件提示
            return "1";
        }
        //构建文件上传所要保存的"文件夹路径"--这里是相对路径，保存到项目根路径的文件夹下
        String realPath = "src/main/resources/" + path;
        //存放上传文件的文件夹
        File file = new File(realPath);
        String oldName = uploadFile.getOriginalFilename();
        String newName = null;
        if (oldName != null) {
            newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        }
        try {
            //构建真实的文件路径
            File newFile = new File(file.getAbsolutePath() + File.separator + newName);
            //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            uploadFile.transferTo(newFile);
            return newName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "2";

    }
    public static void email(String email,int code)throws Exception {
        Properties properties = new Properties();
        properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议
        properties.setProperty("mail.smtp.auth", "true");//需要验证
        //properties.setProperty("mail.debug", "true");//设置debug模式 后台输出邮件发送的过程
        Session session = Session.getInstance(properties);
        session.setDebug(true);//debug模式
        //邮件信息
        Message messgae = new MimeMessage(session);
        messgae.setFrom(new InternetAddress("admin@lglin.com"));//设置发送人
        messgae.setText("你的验证码为："+code+"。请注意，验证码有效时间为2分钟！！！");//设置邮件内容
        messgae.setSubject("邮箱验证");//设置邮件主题
        //发送邮件
        Transport tran = session.getTransport();
//        tran.connect("smtp.sina.com", 25, "邮箱账户", "邮箱授权码");//连接到新浪邮箱服务器
        tran.connect("smtp.qq.com",587, "admin@lglin.com", "oxgrnnsbkforbhdd");//连接到QQ邮箱服务器
        tran.sendMessage(messgae, new Address[]{ new InternetAddress(email)});//设置邮件接收人
        tran.close();


    }
}
